//
// Created by 11401 on 25-7-14.
//

#include "bsp_ats.h"

#include <sys/types.h>

#include "semphr.h"
BSP_ATS522_T ats522;

//初始化
uint8_t ats522_rx_bueff[32];
uint8_t ats522_tx_bueff[32];

 void bsp_ats522_init(UART_HandleTypeDef *huart)
 {
     BSP_ATS522_T *AST522;
     AST522=&ats522;
     memset(AST522, 0, sizeof(ats522));
     AST522->huart=huart;
     AST522->flag=false;
     AST522->rx_buffer=ats522_rx_bueff;
     AST522->tx_buffer=ats522_tx_bueff;
     HAL_UARTEx_ReceiveToIdle_IT(huart,
                                 ats522_rx_bueff,
                                 sizeof(AST522->rx_buffer));
     __HAL_UART_ENABLE_IT(AST522->huart,UART_IT_IDLE);
     __HAL_DMA_DISABLE_IT(&hdma_uart4_rx,DMA_IT_HT);

 }

 //写数据
 bool bsp_ats522_write(uint8_t *data,uint8_t size)
 {

     HAL_UART_Transmit(ats522.huart,data,size,0XFFFF);

     return 0;
 }
uint8_t bsp_ats522_read(uint8_t *rx_data,uint16_t size)
 {
     HAL_UART_Receive(&huart4,rx_data,  size, 0xff);

     return *rx_data;
 }

//计算校验和
uint8_t bsp_ats522_rcc(uint8_t *data,uint8_t size)
 {
     uint8_t rcc=0;
     for (uint8_t i=0;i<=size-2;i++)
     {
            rcc^=data[i];
     }
     rcc=rcc^0xff;
     return rcc;
 }

 //空闲中断读数据
 void bsp_ats522_callback(UART_HandleTypeDef *huart)
 {
     if (huart->Instance==UART4)
     {
         //重新开启接收
         //消息队列
         ats522.flag=true;
         uint8_t  tx_task_ic_flag=0;
         if (ats522_rx_bueff[0]==0x08)
         {
             tx_task_ic_flag='b';
             xQueueSendToBackFromISR(ic_rx_packHandle,"b",pdMS_TO_TICKS(50));
         }
         if (ats522_rx_bueff[0]==0x0A)
         {
             tx_task_ic_flag='c';
             xQueueSendToBackFromISR(ic_rx_packHandle,"c",pdMS_TO_TICKS(50));
         }
         if (ats522_rx_bueff[0]==0x07)
         {
             tx_task_ic_flag='d';
             xQueueSendToBackFromISR(ic_rx_packHandle,"d",pdMS_TO_TICKS(50));
         }
         // if (ats522_rx_bueff[0] == 0x06)
         // {
         //     tx_task_ic_flag = 'e';
         //     xQueueSendToBackFromISR(ic_rx_packHandle, "e", pdMS_TO_TICKS(50));
         // }
         if (ats522_rx_bueff[0]==0x16)
         {
             tx_task_ic_flag='f';
             xQueueSendToBackFromISR(ic_rx_packHandle,"f",pdMS_TO_TICKS(50));
         }



         HAL_UARTEx_ReceiveToIdle_IT(ats522.huart,
                                    ats522_rx_bueff,
                                    sizeof(ats522_rx_bueff));
         __HAL_UART_ENABLE_IT(ats522.huart,UART_IT_IDLE);
         __HAL_DMA_DISABLE_IT(&hdma_uart4_rx,DMA_IT_HT);

     }
 }
//请求
 bool bsp_ast522_Request(void)
 {
     uint8_t data[7]={0x07,0x02,0x41,0x01,0x52,0xE8,0x03};
     bsp_ats522_write(data,7);
     return 0;
 }
//防碰撞
bool bsp_ast522_PiccAnticoll(void)
 {
     uint8_t datax[8]={0x08,0x02,0x42,0x02,0x93,0x00,0x26,0x03};
     bsp_ats522_write(datax,8);
     //读取队列，从机应答
     return 0;
 }
//选择
bool bsp_ats522_PiccSelect(uint8_t id_1,uint8_t id_2,uint8_t id_3,uint8_t id_4)
 {


     uint8_t datax[11]={0x0B,0X02,0X43,0X05,0x93,id_1,id_2,id_3,id_4,0x00,0x03};
     datax[11-2]=bsp_ats522_rcc(datax,11);
     bsp_ats522_write(datax,11);
     //读取队列，从机应答
     return 0;
 }

//暂停
bool bsp_ast522_PiccHalt(void)
 {
    uint8_t datax[8]={0x06,0x02,0x44,0x00,0,0xBF,0x03};
     bsp_ats522_write(datax,8);
     //读取队列，从机应答
     return 0;
 }

//验证密钥
bool bsp_ats522_PiccAuthKey(uint8_t AB,
                            uint8_t id_1,uint8_t id_2,uint8_t id_3,uint8_t id_4,
                            uint8_t key[6],
                            uint8_t Card_block)
 {
    uint8_t send[9+9]={0x12,0x02,0x46,0x0C,
                    AB,
                    id_1,id_2,id_3,id_4,
                    key[0],key[1],key[2],key[3],key[4],key[5],
                    Card_block,0x00,0x03
                    };
     send[18-2]=bsp_ats522_rcc(send,18);
     bsp_ats522_write(send,18);

     //读取队列，从机应答
     return 0;
 }
//读数据
bool bsp_ats522_Read(uint8_t Card_block)
 {
     uint8_t read[7]={0x07,0x02,0x47,0x01, Card_block,0x00,0X03};
     read[7-2]= bsp_ats522_rcc(read,7);
     bsp_ats522_write(read,7);

     //读取队列，从机应答
     return 0;
 }
//写数据
bool bsp_ats522_Write_data(uint8_t Card_block,uint8_t data[16])
 {
     uint8_t write[23]={0x17,0x02,0x48,0x11,Card_block,
                        data[0], data[1], data[2], data[3], data[4], data[5] ,data[6] ,data[7],
                        data[8], data[9], data[10], data[11], data[12], data[13] ,data[14] ,data[15],
                        0xB7,0X03};
     write[23-2]= bsp_ats522_rcc(write,23);
     bsp_ats522_write(write,23);

     //读取队列，从机应答
     return 0;
 }
//块读
bool bsp_ats522_block_Read(uint8_t begen,
                            uint8_t block,
                            uint8_t key_class,
                            uint8_t key[6])
 {
     uint8_t block_read[15]={0x07,0x02,0x52,0x09,
                             begen,
                             block,
                             key_class,
                             key[0],key[1],key[2],key[3],key[4],key[5]};

     block_read[15-2]= bsp_ats522_rcc(block_read,15);
     bsp_ats522_write(block_read,15);

     //读取队列，从机应答
     return 0;
 }
//块写
bool bsp_ats522_block_Wirte(uint8_t begen,
                            uint8_t block,
                            uint8_t key_class,
                            uint8_t key[6],
                            uint8_t data[16])
 {

     uint8_t block_wirte[15+16]={0x07,0x02,0x52,0x09,
                             begen,
                             block,
                             key_class,
                             key[0],key[1],key[2],key[3],key[4],key[5],
                             data[0],data[1],data[2], data[3],  data[4], data[5],data[6],  data[7],
                             data[8],data[9],data[10],data[11],data[12],data[13],data[14],data[15],
                                0x00,0x03};

     block_wirte[31-2]= bsp_ats522_rcc(block_wirte,31);
     bsp_ats522_write(block_wirte,31);


     //读取队列，从机应答
     return 0;

 }
